package tools;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.border.*;
import java.util.*;

/**
 * Title:
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:
 * @author
 * @version 1.0
 */

public class UrlProcess extends AbstractProcess {

  private JTextField htmlUrl;
  private JTextField proxyUrl;
  private JCheckBox useProxy;
  private UrlProcessModel model;
  private JButton getHtmlButton;

  public UrlProcess() {
    setName( "URL" );
    htmlUrl = new JTextField( 40 );
    proxyUrl = new JTextField( 20 );
    useProxy = new JCheckBox();
    getHtmlButton = new JButton( "Get HTML" );

    setModel( new UrlProcessModel() );

    setUseProxy( false );
    setProxyUrl( "http://gate:8080" );
    setHtmlUrl( "http://biz.yahoo.com/fin/l/i/ibm.html" );

    JPanel centerPanel = new JPanel( new FlowLayout( FlowLayout.CENTER ) );

    JPanel urlPanel = new JPanel( new BorderLayout( 5, 5 ) );
    JLabel l = new JLabel( "HTML Url" );
    l.setLabelFor( htmlUrl );
    urlPanel.add( l, BorderLayout.WEST );
    urlPanel.add( htmlUrl, BorderLayout.CENTER );

    JPanel contentPanel = new JPanel( new BorderLayout( 5, 5 ) );
    contentPanel.add( urlPanel, BorderLayout.NORTH );

    JPanel proxyPanel = new JPanel( new GridBagLayout() );
    proxyPanel.setBorder( BorderFactory.createCompoundBorder(
            BorderFactory.createEtchedBorder(),
            BorderFactory.createEmptyBorder( 2, 3, 3, 3 ) ) );
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.anchor = gbc.NORTHWEST;
    gbc.gridx = 0;
    gbc.gridy = 0;
    gbc.gridwidth = 2;
    useProxy.setText( "Use Proxy" );
    proxyPanel.add( useProxy, gbc );

    l = new JLabel( "Proxy Url" );
    l.setLabelFor( proxyUrl );
    gbc.gridwidth = 1;
    gbc.gridy++;
    gbc.gridx = 0;
    gbc.insets = new Insets( 0, 0, 2, 2 );
    proxyPanel.add( l, gbc );

    gbc.gridx++;
    proxyPanel.add( proxyUrl, gbc );

    JPanel p = new JPanel( new FlowLayout( FlowLayout.LEFT ) );
    p.add( proxyPanel );
    contentPanel.add( p, BorderLayout.CENTER );

    p = new JPanel( new FlowLayout( FlowLayout.CENTER ) );

    centerPanel.add( contentPanel );

    add( centerPanel, BorderLayout.CENTER );

    JPanel buttonPanel = new JPanel( new GridLayout() );
    buttonPanel.add( getHtmlButton );
    p.add( buttonPanel );
    add( p, BorderLayout.SOUTH );

    useProxy.addItemListener( new ItemListener(){
      public void itemStateChanged( ItemEvent e ){
        setUseProxy( e.getStateChange() == e.SELECTED );
      }
    } );

    htmlUrl.getDocument().addDocumentListener( new DocumentListener(){
      private void setText(DocumentEvent e){
        Document d = e.getDocument();

        try {
          setHtmlUrlModel( d.getText( 0, d.getLength() ) );
        } catch ( BadLocationException ex ){
          ex.printStackTrace();
        }
      }
      public void insertUpdate( DocumentEvent e ){
        setText( e );
      }
      public void removeUpdate( DocumentEvent e ){
        setText( e );
      }
      public void changedUpdate( DocumentEvent e ){
        setText( e );
      }
    } );

    proxyUrl.getDocument().addDocumentListener( new DocumentListener(){
      private void setText(DocumentEvent e){
        Document d = e.getDocument();

        try {
          setProxyUrlModel( d.getText( 0, d.getLength() ) );
        } catch ( BadLocationException ex ){
          ex.printStackTrace();
        }
      }
      public void insertUpdate( DocumentEvent e ){
        setText( e );
      }
      public void removeUpdate( DocumentEvent e ){
        setText( e );
      }
      public void changedUpdate( DocumentEvent e ){
        setText( e );
      }
    } );

    getHtmlButton.addActionListener( new ActionListener(){
      public void actionPerformed( ActionEvent e ){
        getHtmlPerformed( e );
      }
    });
  }

  protected void getHtmlPerformed( ActionEvent e ){
    fireActionPerformed( new ActionEvent( this, e.ACTION_PERFORMED,
                                            e.getActionCommand() ) );
  }

  public void setUseProxy( boolean b ){
    proxyUrl.setEnabled( b );
    model.setUseProxy( b );
  }

  public boolean getUseProxy(){
    return model.isUseProxy();
  }

  public void setModel( UrlProcessModel model) {
    this.model = model;
  }

  public UrlProcessModel getModel() {
    return model;
  }

  public void setHtmlUrlModel( String hu ){
    model.setHtmlUrl( hu );
  }

  public void setHtmlUrl( String hu ){
    setHtmlUrlModel( hu );
    htmlUrl.setText( hu );
  }

  public void setProxyUrlModel( String hu ){
    model.setProxyUrl( hu );
  }

  public void setProxyUrl( String hu ){
    setProxyUrlModel( hu );
    proxyUrl.setText( hu );
  }
}